<template>
  <!-- 一条文章单元格 -->

  <!-- 反馈面板 -->
  <div class="article-item__container">
    <van-cell border @click="$router.push('/article?art_id='+item.art_id)">
    <!-- 一级面板 -->
    <van-action-sheet
      v-model="show"
      :actions="actions"
      @select="onSelect"
      get-container="body"
      cancel-text="取消"
    />
    <!-- 二级面板 -->
    <van-action-sheet
      v-model="show2"
      :actions="actions2"
      @select="onSelect"
      get-container="body"
      cancel-text="取消"
    />
        <!-- 反馈其他问题 -->
    <van-dialog
      v-model="show3"
      title="反馈其他问题"
      show-cancel-button
      get-container="body"
      @confirm="onConfirmFn"
    >
        <van-field
        v-model.trim="msg"
          type="textarea"
          placeholder="请输入反馈的内容1-100字"
          border
          v-focus
        />

    </van-dialog>

    <van-cell>
      <!-- 标题区域的插槽 -->
      <template #title>
        <div class="title-box">
          <!-- 标题 -->
          <span>{{ item.title }}</span>
          <!-- 单图 -->
          <img
            v-if="item.cover.type === 1"
            class="thumb"
            :src="item.cover.images[0]"
          />
        </div>
        <!-- 三张图片 -->
        <div class="thumb-box" v-if="item.cover.type > 1">
          <img
            class="thumb"
            v-for="(imgurl, index) in item.cover.images"
            :key="index"
            :src="imgurl"
          />
        </div>
      </template>
      <!-- label 区域的插槽 -->
      <template #label>
        <div class="label-box">
          <div>
            <span>{{ item.aut_name }}</span>
            <span>{{ item.comm_count }}评论</span>
            <!-- 时间应该是几个月前 -->
            <span>{{ formatTimeAgoFn(item.pubdate) }}</span>
          </div>
          <!-- 反馈按钮  注册点击事件，让顶部的反馈面板显示出来-->
          <van-icon
          name="cross"
          @click.stop="onCloseClick"
          v-if="isShowCross"
          />
        </div>
      </template>
    </van-cell>
    </van-cell>
  </div>
</template>

<script>
import { formatTimeAgoFn } from '@/utils/date'
import { Toast } from 'vant'
import { FIRST_ACTION_SHEET, SECOND_ACTION_SHEET } from '@/constant'
// import firstActions from '../../../../api/report'
// 调用接口
import { articleDislikeAPI, articleReportsAPI } from '@/api/home'
export default {
  name: 'Articleltem',
  props: {
    item: {
      type: Object,
      required: true
    },
    i: Number,
    isShowCross: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      show: false, // 控制一级
      show2: false, // 控制二级
      show3: false,
      msg: '',
      actions: FIRST_ACTION_SHEET,
      actions2: SECOND_ACTION_SHEET
    }
  },

  methods: {
    formatTimeAgoFn,
    async onConfirmFn () {
      if (this.msg.length === 0) {
        Toast('反馈内容不能为空')
      }
      try {
        await articleReportsAPI({
          artId: this.item.art_id,
          type: '0',
          remark: this.msg
        })
        Toast('反馈成功')
      } catch (e) {
        Toast.fail(e.message || '反馈失败')
      }
    },

    // onSelect () {},
    // 让顶部的反馈面板显示出来
    onCloseClick () {
      this.show = true
    },
    // 点击不感兴趣
    async onSelect (obj) {
      if (obj.id === 112) {
        this.show2 = true
        // 垃圾内容
      } else if (obj.id === 111) {
        this.$emit('del-art', this.i)
        // 发送请求，告诉后台，从数据库中删除
        try {
          await articleDislikeAPI({
            artId: this.item.art_id
          })
          Toast.success('操作成功')
        } catch (e) {
          Toast.fail('操作失败')
        }
      } else if (obj.id === 222) {
        // 拉黑作者
      } else {
        // 二级里面的反馈内容
        if (obj.id === 0) {
          this.show3 = true
          this.show = this.show2 = false
        } else {
          try {
            await articleReportsAPI({
              artId: this.item.art_id,
              type: obj.id + '',
              remark: obj.name
            })
            Toast('反馈成功')
          } catch (e) {
            Toast.fail(e.message || '反馈失败')
          }
        }
      }
    }
  }
}
</script>
<style scoped lang="less">
/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}
</style>
